//
//  CAIMenuIconPluginKit.m
//  xxx
//
//  Created by simon on 2017/10/23.
//  Copyright © 2017年 xxx. All rights reserved.
//

#import "CAIMenuIconPluginKit.h"
#import "UIImageView+WebCache.h"

#ifndef SCREEN_WIDTH
#define SCREEN_WIDTH ([[UIScreen mainScreen] bounds].size.width)
#define SCREEN_HEIGHT ([[UIScreen mainScreen] bounds].size.height)
#endif

#ifndef SCREEN_MAX_LENGTH
#define SCREEN_MAX_LENGTH (MAX(SCREEN_WIDTH, SCREEN_HEIGHT))
#define SCREEN_MIN_LENGTH (MIN(SCREEN_WIDTH, SCREEN_HEIGHT))
#endif

//设置iphone6尺寸比例/竖屏,UI所有设备等比例缩放
#ifndef LCDScale_iPhone6
#define LCDScale_iPhone6(X)    ((X)*SCREEN_MIN_LENGTH/375)
#endif


static CGFloat const ZXMinimumInteritemSpacing = 12.f;//item之间最小间隔
static CGFloat const ZXMinimumLineSpacing = 12.f; //最小行间距
static CGFloat const ZXItemWidth = 45.f;
static CGFloat const ZXItemHeight = 45.f;
static NSString * const reuseTagsCell = @"Cell";

@interface CAIMenuIconPluginKit ()

// 获取在有角标的时候，item的最小安全宽度 = iconSquareSideLength + 2*角标预留显示区域，防止"99+"角标显示不下
@property (nonatomic, readonly) CGFloat safeBadgeMinimumItemWidth;

@property (nonatomic, getter=isHasBadge, assign) BOOL hasBadge;

@property (nonatomic, strong) NSMutableArray *dataMArray;

@end

@implementation CAIMenuIconPluginKit

@synthesize scrollEnabled = _scrollEnabled;

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self commonInit];
    }
    return self;
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self)
    {
        [self commonInit];
    }
    return self;
}

#pragma mark -layoutSubviews

- (void)layoutSubviews
{
    [super layoutSubviews];
}


- (void)commonInit
{
    self.backgroundColor = [UIColor whiteColor];
    self.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    self.minimumInteritemSpacing = ZXMinimumInteritemSpacing;
    self.minimumLineSpacing = ZXMinimumLineSpacing;
    self.columnsCount = 4;
    self.imageViewTopToSuperViewSpace = [MenuItemCell getImageViewToSupViewDefaultTop];
    self.iconSquareSideLength = LCDScale_iPhone6(45.f);
    self.titleLabToImageViewSpace = [MenuItemCell getTitleLabToImageViewDefaultSpace];
    self.titleLabBottomToSupViewSpace = [MenuItemCell getTitleLabToSupViewDefaultBottom];
    self.itemSize = CGSizeMake(LCDScale_iPhone6(ZXItemWidth), LCDScale_iPhone6(ZXItemHeight));

    self.clipsToBounds = YES;
    self.clearsContextBeforeDrawing = YES;
    self.hasBadge = YES;
    [self addSubview:self.collectionView];
    self.collectionView.scrollEnabled = self.scrollEnabled;
    [self addConstraintWithView:self.collectionView toSuperviewItem:self];
//    self.collectionView.backgroundColor = [UIColor orangeColor];
}

#pragma mark - constraint

- (void)addConstraintWithView:(UIView *)item toSuperviewItem:(UIView *)superView
{
    item.translatesAutoresizingMaskIntoConstraints = NO;
    
    NSLayoutConstraint *constraint_top = [item.topAnchor constraintEqualToAnchor:superView.topAnchor constant:0];
    NSLayoutConstraint *constraint_bottom = [item.bottomAnchor constraintEqualToAnchor:superView.bottomAnchor constant:0];
    NSLayoutConstraint *constraint_leading = [item.leadingAnchor constraintEqualToAnchor:superView.leadingAnchor constant:0];
    NSLayoutConstraint *constraint_centerX = [item.centerXAnchor constraintEqualToAnchor:superView.centerXAnchor];
    [NSLayoutConstraint activateConstraints:@[constraint_top,constraint_bottom,constraint_leading,constraint_centerX]];
}

#pragma mark - UICollectionViewDataSource

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataMArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    MenuItemCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseTagsCell forIndexPath:indexPath];
    if (indexPath.item<self.dataMArray.count)
    {
        id data = [self.dataMArray objectAtIndex:indexPath.item];
        if ([self.delegate respondsToSelector:@selector(menuIconView: cell:forItemSetData:cellForItemAtIndexPath:)])
        {
            [self.delegate menuIconView:self  cell:cell forItemSetData:data cellForItemAtIndexPath:indexPath];
        }
        else if([data isKindOfClass:[MenuItemModel class]])
        {
            [cell setData:data placeholderImage:self.placeholderImage];
        }
        else
        {
            NSLog(@"传的数据不对应显示");
        }
    }
    return cell;
}


- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return self.sectionInset;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return self.minimumLineSpacing;
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return self.minimumInteritemSpacing;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeZero;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if ([self.flowLayoutDelegate respondsToSelector:@selector(menuIconCollectionView:layout:sizeForItemAtIndexPath:)])
    {
        return [self.flowLayoutDelegate menuIconCollectionView:collectionView layout:collectionViewLayout sizeForItemAtIndexPath:indexPath];
    }
    return self.itemSize;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{    
    if ([self.delegate respondsToSelector:@selector(menuIconView:collectionView:didSelectItemAtIndexPath:)])
    {
        [self.delegate menuIconView:self collectionView:collectionView didSelectItemAtIndexPath:indexPath];
    }
}

- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    MenuItemCell *iconCell = (MenuItemCell *)cell;
    // 设置中心图标
    iconCell.imageViewSquareSideLength = self.iconSquareSideLength;
    iconCell.titleLabToImageViewSpace = self.titleLabToImageViewSpace;
    iconCell.imageViewTopToSuperViewSpace = self.imageViewTopToSuperViewSpace;
    iconCell.titleLabBottomToSupViewSpace = self.titleLabBottomToSupViewSpace;
    if (self.titleLabelTextColor) {
        iconCell.titleLab.textColor = self.titleLabelTextColor;
    }
    if (self.titleLableFont) {
        iconCell.titleLab.font = self.titleLableFont;
    }
    if ([self.delegate respondsToSelector:@selector(menuIconView:willDisplayCell:forItemAtIndexPath:)])
    {
        [self.delegate menuIconView:self willDisplayCell:iconCell forItemAtIndexPath:indexPath];
    }
}



#pragma mark - UICollectionView

- (UICollectionView *)collectionView
{
    if (!_collectionView)
    {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.sectionInset = self.sectionInset;
        flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;
        self.collectionFlowLayout = flowLayout;
        
        UICollectionView *collection = [[UICollectionView alloc] initWithFrame:self.bounds collectionViewLayout:flowLayout];
        collection.directionalLockEnabled = YES;
        collection.backgroundColor = [UIColor clearColor];
        collection.delegate = self;
        collection.dataSource = self;
        collection.showsVerticalScrollIndicator = NO;
        collection.showsHorizontalScrollIndicator = NO;
//        [collection registerNib:[UINib nibWithNibName:NSStringFromClass([MenuItemCell class]) bundle:nil] forCellWithReuseIdentifier:reuseTagsCell];
        [collection registerClass:[MenuItemCell class] forCellWithReuseIdentifier:reuseTagsCell];

        
        _collectionView = collection;
    }
    return _collectionView;
}


- (NSMutableArray *)dataMArray {
    if (!_dataMArray) {
        _dataMArray = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataMArray;
}


#pragma mark -setter/getter

- (void)setScrollEnabled:(BOOL)scrollEnabled
{
    _scrollEnabled = scrollEnabled;
    self.collectionView.scrollEnabled = scrollEnabled;
}

- (BOOL)isScrollEnabled{
    return _scrollEnabled;
}

- (void)setScrollDirection:(UICollectionViewScrollDirection)scrollDirection
{
    _scrollDirection = scrollDirection;
    self.collectionFlowLayout.scrollDirection = scrollDirection;
    if (_scrollDirection == UICollectionViewScrollDirectionVertical) {
        self.collectionView.alwaysBounceVertical = YES;
    }
}

- (void)setSectionInset:(UIEdgeInsets)sectionInset
{
    _sectionInset = sectionInset;
    self.collectionFlowLayout.sectionInset = sectionInset;
}

- (void)setMinimumLineSpacing:(CGFloat)minimumLineSpacing
{
    _minimumLineSpacing = minimumLineSpacing;
    self.collectionFlowLayout.minimumLineSpacing = minimumLineSpacing;
}


- (void)setBackgroundColor:(UIColor *)backgroundColor{
    
    self.collectionView.backgroundColor = backgroundColor;
}

- (CGFloat)safeBadgeMinimumItemWidth
{
    if (self.hasBadge)
    {
        if (SCREEN_MIN_LENGTH == 320.0)
        {
            return self.iconSquareSideLength + 33;
        }
        return self.iconSquareSideLength + LCDScale_iPhone6(40);
    }
    return self.iconSquareSideLength;
}

- (BOOL)isHasBadge{
    return _hasBadge;
}



#pragma mark - 获取最小安全宽度，最小安全高度；

- (CGSize)getItemMiniSafeSizeWithTotalWidth:(CGFloat)totalWidth
                               columnsCount:(NSInteger)count
                               sectionInset:(UIEdgeInsets)inset
                    minimumInteritemSpacing:(CGFloat)minimumInteritemSpacing
                                   hasBadge:(BOOL)flag
                       iconSquareSideLength:(CGFloat)iconEqualSideLength
                   titleLabToImageViewSpace:(CGFloat)space
{
    self.hasBadge = flag;
    CGFloat itemWidth =  (totalWidth - (count - 1) * minimumInteritemSpacing - inset.left - inset.right)/count;
    CGFloat safeWidth =  floorf(itemWidth) <= floorf(self.safeBadgeMinimumItemWidth) ? floorf(self.safeBadgeMinimumItemWidth) : floorf(itemWidth);
    // 如果item的理论计算宽度 比 实际宽度小，则重新调整item的间距；
    if (itemWidth < self.safeBadgeMinimumItemWidth) {
        self.minimumInteritemSpacing = (totalWidth - count* self.safeBadgeMinimumItemWidth - inset.left - inset.right)/(count-1);
    }
    CGFloat safeHeight = (self.imageViewTopToSuperViewSpace + iconEqualSideLength + space + LCDScale_iPhone6(17) + self.titleLabBottomToSupViewSpace);
    return CGSizeMake(safeWidth, safeHeight);
}

#pragma mark - 计算方法

- (NSInteger)getRowsWithDataCount:(NSInteger)count
{
    if (count ==0)
    {
        return 0;
    }
    NSInteger rows = 0; // 行数
    //计算有几行的简单方法
    if (count % self.columnsCount > 0)
    {
        //假装填满
        NSInteger totalItems = count + (self.columnsCount - (count % self.columnsCount));
        rows = totalItems/self.columnsCount;
    }
    else
    {
        rows = count/self.columnsCount;
    }
    return  rows;
}

- (CGFloat)getCellHeightWithContentData:(NSArray *)data
{
    if ([data count]==0)
    {
        return 0.f;
    }
    CGFloat height = 0;
    NSInteger rows = [self getRowsWithDataCount:data.count];
    //计算高度
    height = rows * self.itemSize.height + (rows - 1) * self.minimumLineSpacing + self.sectionInset.top + self.sectionInset.bottom;
    NSLog(@"个数= %@,高度 = %@",@(data.count), @(height));
    
    return ceilf(height);
}


- (void)setData:(NSArray *)data
{
//    不管是浅拷贝还是深拷贝，在返回一个新的容器集合对象时，容器内的元素仍然是浅拷贝；
//    不适合修改某个元素的属性，重新刷新数据的场景，data依然=self.dataMArray；
//    if (![self.dataMArray isEqualToArray:data])
//    {
//    }
    [self.dataMArray removeAllObjects];
    [self.dataMArray addObjectsFromArray:data];
    [self.collectionView reloadData];
}


@end
